#include "additem.h"
#include "ui_additem.h"

AddItem::AddItem(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::AddItem)
{
    ui->setupUi(this);

    table=REF_CPU;
    ui->memType->addItems(memTypes);
    ui->memType->setCurrentIndex(memTypes.size() - 1);

    intelFamily=sp_getCategory("FirstName",table,"Company","Intel");
    intelSockets=sp_getCategory("Sockets",table,"Company","Intel");
    amdFamily=sp_getCategory("FirstName",table,"Company","AMD");
    amdSockets=sp_getCategory("Sockets",table,"Company","AMD");
}

AddItem::~AddItem()
{
    delete ui;
}

void AddItem::on_btn_submit_clicked()
{
    if(!ui->AMD->isChecked() && !ui->intel->isChecked())
        return;

    QString company;
    if(ui->AMD->isChecked())
        company="AMD";
    else
        company="Intel";

    QString firstName=ui->FirstName->currentText();
    if(firstName.isEmpty())
        return;

    QString lastName=ui->lastName->text();
    if(lastName.isEmpty())
        return;

    int lithography=ui->lithography->value();
    int cores=ui->cores->value();
    int threads=ui->threads->value();
    double base=ui->base->value();
    double turbo=ui->turbo->value();
    int cache=ui->cache->value();
    int tdp=ui->TDP->value();
    QString memType=ui->memType->currentText();
    if(memType.isEmpty())
        return;

    int memFreq=ui->memFreq->value();
    QString graphics=ui->graphics->text();
    if(graphics.isEmpty())
        graphics="None";

    QString sockets=ui->Sockets->currentText();

    QSqlQuery query;
    query.prepare(QString("insert into %1 (Company,FirstName,LastName,Lithography,"
                          "Cores,Threads,Base,Turbo,Cache,TDP,"
                          "MemType,MemFreq,Graphics,Sockets) values (:company,:firstName,:lastName,:lithography,"
                          ":cores,:threads,:base,:turbo,:cache,:tdp,"
                          ":memType,:memFreq,:graphics,:sockets)").arg(table));
    query.bindValue(":company",company);
    query.bindValue(":firstName",firstName);
    query.bindValue(":lastName",lastName);
    query.bindValue(":lithography",lithography);
    query.bindValue(":cores",cores);
    query.bindValue(":threads",threads);
    query.bindValue(":base",base);
    query.bindValue(":turbo",turbo);
    query.bindValue(":cache",cache);
    query.bindValue(":tdp",tdp);
    query.bindValue(":memType",memType);
    query.bindValue(":memFreq",memFreq);
    query.bindValue(":graphics",graphics);
    query.bindValue(":sockets",sockets);
    if(query.exec())
        accept();
    else
    {
        qDebug() << query.lastError().text();
        MESSAGE_CANNOT_SUBMIT
    }
}

void AddItem::on_intel_toggled(bool checked)
{
    if(checked)
    {
        ui->AMD->setChecked(false);
        ui->FirstName->clear();
        ui->FirstName->addItems(intelFamily);
        ui->Sockets->clear();
        ui->Sockets->addItems(intelSockets);
    }
    else
        ui->AMD->setChecked(true);
}

void AddItem::on_AMD_toggled(bool checked)
{
    if(checked)
    {
        ui->intel->setChecked(false);
        ui->FirstName->clear();
        ui->FirstName->addItems(amdFamily);
        ui->Sockets->clear();
        ui->Sockets->addItems(amdSockets);
    }
    else
        ui->intel->setChecked(true);
}
